package com.xpjhs.user.service;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xpjhs.user.model.SexType;
import com.xpjhs.user.model.User;
import com.xpjhs.util.ExcelReadUtil;

@Service
public class ImportExcelService {

    @Resource
    private UserService userService;
	/**
	 * 导入数据
	 * @throws IOException 
	 */
	public void importExcel(InputStream inputStream) throws InvocationTargetException, IllegalAccessException, IOException {
		FileInputStream inputStream2=new FileInputStream(new File("f:/2.xlsx"));
        List<List<Object>> dataList = reciveSheet(inputStream2);
        for (List<Object> list : dataList) {
        	User user=new User();
        	String qq=(list.get(2)+"").replace(",", "");
        	user.setRealName(list.get(0)+"");//真实姓名
        	user.setCardName(list.get(1)+"");
        	user.setQq(qq);
        	user.setEmail(qq+"@qq.com");
        	user.setSexType(SexType.男);
        	user.setAge(list.get(4)+"");
        	userService.saveUser(user);
		}
        
        //导入用户操作

	}


    private List<List<Object>> reciveSheet(InputStream inputStream) throws IOException {
        ExcelReadUtil excelRead = new ExcelReadUtil();
        excelRead.importExcel(inputStream);
        int count = excelRead.getRowIndex(0);
        List<List<Object>> dataList = new ArrayList<List<Object>>();
        for (int i = 0; i < count; i++){
            dataList.add(excelRead.readRow(0, i));
        }

        return dataList;
    }
    
}
